//
//  SharingAddController.swift
//  健身宝
//
//  Created by hzt on 2017/10/17.
//  Copyright © 2017年 hzt. All rights reserved.
//

import UIKit

class PhotoAddController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate {
    var index = 0
    var addBtn:UIButton!
    var backBtn:UIButton!
    var postBtn:UIButton!
    var shape:CAShapeLayer!
    var IndexPaths:[IndexPath]?
    var textView:UITextView!
    var image:UIImage!
    
    override func viewDidLoad(){
        super.viewDidLoad()
        
        viewConfig()
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
        
        self.navigationController?.isNavigationBarHidden = true
        
        shape = CAShapeLayer()
        shape.frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 230)
        shape.backgroundColor = UIColor.white.cgColor
        shape.actions = ["bounds":NSNull(),"position":NSNull(),"path":NSNull()]
        view.layer.addSublayer(shape)
        
//        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height: 64)))
//        navBar.backgroundColor =  view.backgroundColor
//        view.addSubview(navBar)
        
         backBtn = UIButton(frame: CGRect(x: 16, y: 23, width: 35, height: 30))
        backBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        backBtn.setTitle("返回", for: .normal)
        backBtn.setTitleColor(.black, for: .normal)
        view.addSubview(backBtn)
        
        postBtn = UIButton(frame: CGRect(x: UIScreen.main.bounds.width-51, y: 23, width: 35, height: 30))
        postBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        postBtn.setTitle("发表", for: .normal)
        postBtn.setTitleColor(.black, for: .normal)
        view.addSubview(postBtn)
        
        textView = UITextView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 125))
        textView.backgroundColor = UIColor.white
        textView.showsVerticalScrollIndicator = false
        textView.font = UIFont.systemFont(ofSize: 16)
        view.addSubview(textView)
        
        addBtn = UIButton(frame: CGRect(x: 15, y: 199, width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4))
        addBtn.backgroundColor = UIColor.white
        addBtn.layer.borderWidth = 1
        addBtn.layer.borderColor = UIColor.lightGray.cgColor
        addBtn.setTitle("+", for: .normal)
        addBtn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
        addBtn.setTitleColor(.lightGray, for: .normal)
        addBtn.adjustsImageWhenHighlighted = false
        view.addSubview(addBtn)
        
        addBtn.addTarget(self, action: #selector(addImage), for: .touchUpInside)
        backBtn.addTarget(self, action: #selector(back), for: .touchUpInside)
        postBtn.addTarget(self, action: #selector(post), for: .touchUpInside)
    }
    
    @objc func post(){
        let parameter:NSDictionary = [
            "SessionID":LoginModel.sessionID,
            "mod":"shaitu",
            "title":textView.text!,
            "userid":LoginModel.userID,
            "club_id":NSNull()
        ]
        

        RONetworkMngTool.shared.postSharing(parameter: parameter, image: image) {
            self.back()
        }
        }
    
    @objc func addImage(){

        _ = presentHGImagePicker(maxSelected: 9, indexPaths: IndexPaths, completeHandler: { (photos,indexPaths) in
            let index = photos.count
            let padding = (UIScreen.main.bounds.width-60)/4+5
            
            self.image = photos[0].turnToUIImage(size: CGSize(width: 50, height: 50))
            self.IndexPaths = indexPaths
            self.removeImage()
            
            switch index {
            case 1...4:
                self.addBtn.frame.origin = CGPoint(x: 15+CGFloat(index)*padding, y: 199)
                self.shape.frame.size.height = 230
                
                if index == 4{
                    self.shape.frame.size.height = 320
                    self.addBtn.frame.origin = CGPoint(x: 15, y: 199+padding)
                }
                
                for n in 1...index{
                    let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                    imgView.frame.origin = CGPoint(x: 15+CGFloat(n-1)*padding, y: 199)
                    imgView.image = photos[n-1].turnToUIImage(size: imgView.bounds.size)
                    self.view.addSubview(imgView)
                }
                
            case 5...8:
                self.addBtn.frame.origin = CGPoint(x: 15+CGFloat(index-4)*padding, y: 199+padding)
                
                self.shape.frame.size.height = 230 + padding
                
                if index == 8{
                    self.shape.frame.size.height = 410
                    self.addBtn.frame.origin = CGPoint(x: 15, y: 199+180)
                }
                
                for n in 1...4{
                    let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                    imgView.frame.origin = CGPoint(x: 15+CGFloat(n-1)*padding, y: 199)
                    imgView.image = photos[n-1].turnToUIImage(size: imgView.bounds.size)
                    self.view.addSubview(imgView)
                }
                
                for n in 5...index{
                    let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                    imgView.frame.origin = CGPoint(x: 15+CGFloat(n-5)*padding, y: 199+padding)
                    imgView.image = photos[n-1].turnToUIImage(size: imgView.bounds.size)
                    self.view.addSubview(imgView)
                }
                
            default:
                let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                imgView.frame.origin = CGPoint(x: 15, y: 199+padding*2)
                imgView.image = photos[8].turnToUIImage(size: imgView.bounds.size)
                self.view.addSubview(imgView)
                
                self.addBtn.frame.origin = CGPoint(x: 15+padding, y: 199+padding*2)
                self.shape.frame.size.height = 230 + padding*2
                
                for n in 1...4{
                    let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                    imgView.frame.origin = CGPoint(x: 15+CGFloat(n-1)*padding, y: 199)
                    imgView.image = photos[n-1].turnToUIImage(size: imgView.bounds.size)
                    self.view.addSubview(imgView)
                }
                
                for n in 5...8{
                    let imgView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: (UIScreen.main.bounds.width-60)/4, height: (UIScreen.main.bounds.width-60)/4)))
                    imgView.frame.origin = CGPoint(x: 15+CGFloat(n-5)*padding, y: 199+padding)
                    imgView.image = photos[n-1].turnToUIImage(size: imgView.bounds.size)
                    self.view.addSubview(imgView)
                }
            }
        })
    }
    
    func removeImage(){
        for subView in view.subviews{
            if subView.isKind(of: UIImageView.self){
                subView.removeFromSuperview()
            }
        }
    }
    
    @objc func back(){
        _ = navigationController?.popViewController(animated: true)
        self.navigationController?.isNavigationBarHidden = false
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textView.resignFirstResponder()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}

